using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using PHMEE.Admin;
using Serilog;
using Serilog.Events;

namespace PHMEEAdmin;

public class Program
{
    public async static Task<int> Main(string[] args)
    {
        Log.Logger = new LoggerConfiguration()
#if DEBUG
            .MinimumLevel.Debug()
#else
            .MinimumLevel.Information()
#endif
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            // 为 DataSyncShipService 配置单独的日志文件
            .WriteTo.Logger(lc => lc
                .Filter.ByIncludingOnly(evt => evt.Properties.ContainsKey("SourceContext") 
                                               && evt.Properties["SourceContext"].ToString().Contains("PHMEE.Admin.Services.DataSync.DataSyncShipService"))
                .WriteTo.Async(c => c.File("Logs/DataSyncShip/datasync-ship-.txt", 
                    rollingInterval: RollingInterval.Day)))
            // 为 DataSyncShoreService 配置单独的日志文件
            .WriteTo.Logger(lc => lc
                .Filter.ByIncludingOnly(evt => evt.Properties.ContainsKey("SourceContext") 
                                               && evt.Properties["SourceContext"].ToString().Contains("PHMEE.Admin.Services.DataSync.DataSyncShoreService"))
                .WriteTo.Async(c => c.File("Logs/DataSyncShore/datasync-shore-.txt", 
                    rollingInterval: RollingInterval.Day)))
            // 为 DataSyncController 配置单独的日志文件
            .WriteTo.Logger(lc => lc
                .Filter.ByIncludingOnly(evt => evt.Properties.ContainsKey("SourceContext") 
                                               && evt.Properties["SourceContext"].ToString().Contains("PHMEE.Admin.Controllers.DataSync.DataSyncController"))
                .WriteTo.Async(c => c.File("Logs/DataSyncController/datasync-controller-.txt", 
                    rollingInterval: RollingInterval.Day)))
            // 其他日志继续写入原来的文件
            .WriteTo.Async(c => c.File("Logs/logs-.txt", rollingInterval: RollingInterval.Day))
            .WriteTo.Async(c => c.Console())
            .CreateLogger();

        try
        {
            Log.Information("Starting PHMEEAdmin.HttpApi.Host.");
            var builder = WebApplication.CreateBuilder(args);
            builder.Host.AddAppSettingsSecretsJson()
                .UseAutofac()
                .UseSerilog();
            await builder.AddApplicationAsync<PHMEEAdminHttpApiHostModule>();
            var app = builder.Build();
            await app.InitializeApplicationAsync();
            await app.RunAsync();
            return 0;
        }
        catch (Exception ex)
        {
            if (ex is HostAbortedException)
            {
                throw;
            }

            Log.Fatal(ex, "Host terminated unexpectedly!");
            return 1;
        }
        finally
        {
            Log.CloseAndFlush();
        }
    }
}
